package com.zyy.jomap.back;

import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.GenericVisitorAdapter;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import java.util.List;
import java.util.Optional;

public class MethodVisitor extends GenericVisitorAdapter<Void,Void> {

    /**
     * 方法调用
     * @param n
     * @param arg
     * @return
     */
    @Override
    public Void visit(MethodCallExpr n, Void arg){
        System.out.println("function call: " + n.toString());
        return super.visit(n, arg);
    }

    /**
     * 方法
     * @param n
     * @param arg
     * @return
     */
    @Override
    public Void visit(MethodDeclaration n, Void arg) {
        System.out.println("function declaration: " + n.getNameAsString());
        return super.visit(n, arg);
    }

    /**  OK   单独的类
     * 获取类级别的变量
     */
    private static class ClassVisitor extends VoidVisitorAdapter<Void> {
        @Override
        public void visit(ClassOrInterfaceDeclaration n, Void arg) {
            System.out.println("class fields>>>>>>>>>>"+n.getFields());
            System.out.println("class name:"+n.getFullyQualifiedName().get());
            System.out.println("class extend type:"+n.getExtendedTypes().getFirst());
            NodeList<AnnotationExpr> annoList=n.getAnnotations();
            Optional<AnnotationExpr> annotationExprOptional= n.getAnnotationByName("RestController");
            Optional<AnnotationExpr> dataOptional= n.getAnnotationByName("Data");
            super.visit(n, arg);
        }

        @Override
        public void visit(MethodDeclaration method,Void arg){
            System.out.println("methodName:"+method.getNameAsString());
            NodeList<Parameter> paramList=method.getParameters();
            super.visit(method,arg);
        }
    }

    private static class MethodNameCollector extends VoidVisitorAdapter<List<String>> {

        @Override
        public void visit(MethodDeclaration md, List<String> collector) {
            super.visit(md, collector);
            collector.add(md.getNameAsString());
        }
    }
}
